/**
 * user-mgr.js
 */
define('user-mgr',['jquery-1.3.2','base','liger.all'],function(require, exports, module){
	var Base = require("base");
	var $ = require("jquery-1.3.2");
	require("liger.all")($);
	var ctx = Base.common.utils.getContextPath();
	
	/**
	 * 初始化表单按钮
	 */
	function initFormBtns(){
		
		var btns = [];
		btns.push({ text:'保存', click : function() {
    			var form = liger.get("form");
    			var data = form.getData();
    			var param = {data:liger.toJSON(data)};
    	        $.ajax({
		    		url: ctx+ '/usr/save.do',
		    		type : 'POST',
		    		dataType : 'json',
		    		data : param,
		    		success : function(backData) {
		    			if(backData){
		    				$dataGrid.reload();
		    				$form.setEnabled(["name","sex", "email", "phoneNumber","age", "idCardNo", "type"], true);
		    			}
		    		}
    	        });
    		}
        });
		
		btns.push({ text:'清空', click : function() {
			var form = liger.get("form");
	        form.setData({id:'',name:'',sex:'1',phoneNumber:'', email:'',age:'',type:'1',idCardNo:''});
	        
			}
		});
		
		return btns;
	}
	var $form = $("#form").css('margin',10).ligerForm({
		                 inputWidth: 170, labelWidth: 90, space: 40,
		                  fields: [
		                  { name: "id", type: "hidden" },
		                  { display: "用户名称", name: "name", newline: true, type: "text" , group: "用户信息", groupicon: 'groupicon'}, 
		                  { display: "性别", name: "sex", newline: false, type: "select" , options:{ data : [{text:'男',id:1},{text:'女',id:0}]}  },
		                  { display: "电子邮件", name: "email", newline: false, type: "text"  },
		                  { display: "电话号码", name: "phoneNumber", newline: false, validate:{ required: true, minlength: 11},  type: "text"  },
		                  { display: "年龄", name: "age", newline: false, type: "spinner",options:{ height: 22, type: 'int',isNegative:false }  },
		                  { display: "证件号", name: "idCardNo", newline: false, type: "text", validate:{ required: true, minlength: 18}  },
		                  {   display: "角色", name: "roleId", textField: "roleName",
		                      newline: false, type: "combobox", editor: { parms:[],
		                          selectBoxWidth: 300,
		                          selectBoxHeight: 300,
		                          textField: 'name',
		                          valeuField: 'id',
		                          condition: {
		                              fields: [
		                                  { label: '角色名称', name: 'name', type: 'text' ,width:100}
		                              ]
		                          },
		                          grid: { columns: [
						                  { display: '角色名称', name: 'name', align: 'left', width: 120, minWidth: 100 },
						                  { display: '角色类型', name: 'type', width: 120, minWidth: 100 },
						                  { display: '主键', name: 'id', minWidth: 0, hide:true }],
						                  url:ctx + "/role/fetch.do",root:'datas', pageSize: 10, isScroll: false, sortName: 'name',
		                              width: 240
		                          }
		                      }},
			                  { display: "类型", name: "type", newline: false, type: "select", options:{ data : [{text:'内部用户',id:'1'},{text:'测试用户',id:'2'},{text:'外部用户',id:'0'}]}   }
		                  ]
						}); 
	var param = {};
	var $dataGrid = $("#dataGrid").css('margin',10).ligerGrid({
		url: ctx + "/usr/fetch.do",
		root:'datas',
        columns: [ 
        { display: '主键', hide:true, name:'id', align: 'left', width: 120 } ,
        { display: '用户名称', name: 'name', minWidth: 150 },
        { display: '性别', name: 'sex', width: 150, align: 'center',render:function(rowData){
        	if(rowData.sex){
        		return '男'
        	}
        	return '女'
        }},
        { display: '电子邮件', name: 'email', width: 150, align: 'left'},
        { display: '电话号码', name: 'phoneNumber', width: 150, align: 'right'},
        { display: '年龄', name: 'age', width: 150, align: 'center'},
        { display: '证件号', name: 'idCardNo', width: 150, align: 'left'},
        { display: '类型', name: 'type', width: 150, align: 'center',render: function (item){
        	
        	var typeName = "";
        	switch(item.type){
        	
	        	case 0:  typeName = '外部用户'; break;
	        	case 1:  typeName = '内部用户'; break;
	        	case 2:  typeName = '测试用户'; break;
        	}
        	return   typeName;
        	
        }}
        ], data: [], pageSize: 20, sortName: 'id',
        width: '98%', height: '90%', checkbox: false, rownumbers:false,
        fixedCellHeight:false,
        toolbar:{ items: [ { text: '查询', click: query , icon:'search'}, { line:true },
                           { text: '编辑', click: modify , icon:'modify'}, { line:true },
                           { text: '删除',click: del , icon:'delete'}, { line:true },
                           { text: '授权', click: assignAuthorities , icon:'msn'}, { line:true },
                           { text: '权限管理', click: showAuthorityManagement , icon:'settings'}, { line:true }
                           ]
                       ,height:50},
                       detail: { onShowDetail: showUserAuthority, height:'88%',
                    	   	onCollapse:function(){console.log('onCollapse');
                    	   	return true;
                    	   },onExtend :function(){console.log('onExtend');  return true;} } 
    	});
	
	/**
	 * 查询
	 */
	function query(){
	   	var form = liger.get("form");
    	var $gridManager = $("#dataGrid").ligerGetGridManager();
    	$gridManager.set({ parms:{param:liger.toJSON(form.getData())} });
    	$gridManager.loadData();

	} 
	
	/**
	 * 修改
	 */
	function modify(){
		
	   	var row = $dataGrid.getSelectedRow();
    	var url = ctx + "/usr/toEdit.do";
    	if(row && row.id){
    		url += "?id=" + row.id;
    	}
    	$.ligerDialog.open({ url:url, width: 500, height: 480, title:"编辑" }); 
 
	}
	
	/**
	 * 删除记录
	 */
	function del(){
		
		$.ligerDialog.confirm('是否删除所选用户记录, 若删除记录会把关联记录也删除!', function (yes){
			if(yes){//做成假删除，一年后才能直正删除
				var rows =  $dataGrid.getSelectedRows();
				var param = {user:liger.toJSON(rows)};
		        $.ajax({
		    		url: ctx+ '/usr/delete.do',
		    		type : 'POST',
		    		dataType : 'json',
		    		data : param,
		    		success : function(backData) {
		    			if(backData){
		    				$.ligerDialog.tip({  title: '提示信息',content:'记录已经删除！'});
		    				$dataGrid.reload();
		    			}
		    		}
		        });
			}
		});
	}
	
	/**
	 * 授权
	 */
	function assignAuthorities(){
		$.ligerDialog.open({ url: ctx+'/auth/usrAuthority.do',title:'授权', width: 1024, height:768, modal:true });
		
	}
	
	/**
	 * 权限管理
	 */
	function showAuthorityManagement(){
		
		var usrGridManager = $("#dataGrid").ligerGetGridManager();
		var usr = usrGridManager.getSelectedRow();
		
		if( !(usr && usr.id)){
			$.ligerDialog.warn("请选勾选用户!");
			return;
		}
		var url = ctx+'/auth/authorityManagement.do?id=' + usr.id + "&usrOrRole=0";
		$.ligerDialog.open({ url: url, title:'权限管理', width: 1024, height:768, modal:true });
	}
	
	function assignRole(){
		var data = $dataGrid.getSelectedRow();
		
		//TODO 弹窗，选角色，然后勾选对应的角色
		var form = liger.get("form");
		form.setData(data);
//		$form.setEnabled(["name","sex", "email", "phoneNumber","age", "idCardNo", "type"], false);
		$form.setVisible(["roleId"], true);
	}

	 //显示用户拥有权限
	 function showUserAuthority(row, detailPanel,callback){
	            var grid = document.createElement('div'); 
	            $(detailPanel).css('margin',10).append(grid);
	
				var refId = row.id;
	            $(grid).ligerGrid({
					url: ctx + "/auth/showAuthorities.do",
					root:'datas',
					parms:{refId:row.id,refType:1},
			        columns: [ 
			        { display: '主键', hide:true, name:'id', align: 'left', width: 120 } ,
			        { display: '名称', name: 'name',id:"name", minWidth: 150 },
			        { display: 'url', hide:true, name: 'url', width: 150, align: 'left'},
			        { display: '上级', hide:true, name: 'parent_id', width: 150, align: 'left'},
			        { display:'已赋权标志', width: 150},
			        { display: '类型', name: 'type', width: 150, align: 'center', 
			          render: function (item){
			        	var typeName = "";
			        	switch(item.type){
				        	case 0:  typeName = '默认'; break;
				        	case 1:  typeName = '按钮'; break;
				        	case 2:  typeName = '页面'; break;
				        	case 3:  typeName = '链接'; break;
			        	}
			        	return   typeName;
			        }},
			        {display:'操作', isAllowHide: false, width: 150, align: 'left', render:function(rowData, index, value){
			        	
			        	var json = [];
			        	json.push("<a href='javascript:void(0);' name='op' data-id='" + rowData.id + "' ref-id='"+refId+"'>添加权限</a>");
			        	json.push("|<a href='javascript:void(0);' name='give' data-id='" + rowData.id + "' ref-id='"+refId+"'>赋权</a>");
			        	return json.join('');
			        }}
			        ], data: [], pageSize: 20, sortName: 'id',
			        width: '88%', height: '88%', checkbox: true, rownumbers:false,
			        fixedCellHeight:false, tree: { columnId: 'name' },
			        onAfterShowData:function(data){
			        	$("[name='op']").click(function(){
			        		var $this = $(this);
			        		alert($this.attr('data-id'));
			        	});
			        	
			        	$("[name='give']").click(function(){
			        		var $this = $(this);
			        		assignAuthority($this.attr('data-id'), $this.attr('ref-id'));
			        	});
			        }
			    });
	      }
	
	
	//赋权
	function assignAuthority(id,refId){
		
		var param = {refId:refId,authorityId:id,refType:1};
        $.ajax({
    		url: ctx+ '/auth/assignAuthority.do',
    		type : 'POST', 
    		dataType : 'json',
    		data : param,
    		success : function(backData) {
    			if(backData){
    				//
    				console.log("赋权成功")
//    				window.parent.jQuery("#dataGrid").ligerGetGridManager().reload();
    			}}});
		
	}

	
	
    $("#pageloading").hide();

});